const extractInputContext = (mainString: string, searchTerm: string) => {
  let indexOfInput = mainString.indexOf(searchTerm);
  if (indexOfInput !== -1) {
    let beforeInput = mainString.substring(0, indexOfInput); // 提取 "input" 前面的内容
    let afterInput = mainString.substring(indexOfInput + searchTerm.length); // 提取 "input" 后面的内容
    return { before: beforeInput, after: afterInput };
  } else {
    return { before: "", after: "" }; // 如果未找到匹配的子字符串，返回空字符串
  }
};
const findPositions = (mainString: string, before: string, after: string) => {
  let beforeIndex = mainString.indexOf(before);
  let afterIndex = mainString.indexOf(after);
  if (beforeIndex === -1 || afterIndex === -1) {
    return -1;
  }
  if (beforeIndex > afterIndex) {
    return -1;
  }
  return { beforeIndex: beforeIndex + before.length, afterIndex };
};
export const templateMate = (
  str: string,
  template: string,
  content: string,
) => {
  const result = extractInputContext(template, content);
  const resultIndex = findPositions(str, result.before, result.after);
  if (resultIndex === -1) {
    return;
  }
  return str.substring(resultIndex.beforeIndex, resultIndex.afterIndex);
};
